// TOWN SCRIPT
//    Town 11: Dark Cave

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 11, 3 = trap taken care of
// 11, 6 = bandit leader dead
//

begintownscript;

variables;

short i, lvl, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;

break;

beginstate 10;
	if (get_flag(11, 1) == 0) {
		set_flag(11, 1, 1);
		message_dialog("Ah ha!  The bandits are using this room to store treasure in.",
			"There still appears to be some here.");
	}
break;

beginstate 11;
	play_sound(107);
	message_dialog("You open the chest and find a candlestick nestled in a swath of cloth with the name 'Jennie' embossed in the base.", "You take it and search the chest but there is nothing else there.");
	change_spec_item(4, 1);
break;

beginstate 12;
	if (get_flag(11, 8) > 0) {
		set_flag(11, 4, 1); // monsters gone...
	}
	if (get_flag(11, 4) == 0) {
		set_flag(11, 4, 1);
		message_dialog("You hear some strange, almost icy, hissing ahead.", "");
	}
break;

beginstate 13;
	if (get_flag(11, 5) == 0) {
		set_flag(11, 5, 1);
		message_dialog("This dark cave seems to be a perfect place for bandits to hide out in.",	"");
	}
break;

beginstate 14;
	// bandit leader is dead...
	set_flag(11, 8, 1);
	clear_town();
	message_dialog("As the leader dies, all of his followers run away.", "Now that the bandit leader is dead, the cave and surrounding area seems a litte safer now.");
break;

beginstate 15;
	if (get_flag(11, 7) == 0) {
		set_flag(11, 7, 1);
		message_dialog("You've stumbled into the leaders chamber.", "He doesn't look pleased.");
	}
break;

beginstate 16;
		reset_dialog();
		add_dialog_str(0, "On a pedestal tucked away in a corner, you find a book of spells titled 'My Favorites'.", 0);
		add_dialog_str(1, "It looks like a mage put it here and forgot to pick it back up.", 0);
		add_dialog_choice(0, "Leave it alone.");
		add_dialog_choice(1, "Try to read it.");
		choice = run_dialog(1);
		if (choice == 2) {
			// rune reading between 20 and 30
			if (get_skill_total(42) >= (get_ran(1, 1, 11) + 19)) {
				set_state_continue(17);
			}
			else {
				message_dialog("The runes in the book seem incomprehensible.",
					"Your head spins from trying to read it.");
				// maybe confused now
				i = 0;
				while (i < 4) {
					if (char_ok(i))
						set_char_status(i, 18, 10, 0, 1);
					i = i + 1;
				}
			}
		}
break;

beginstate 17;
	// give spells (up to level 4)...
	// - if someone wants, they can read this to their hearts content...
	play_sound(28);
	reset_dialog();
	add_dialog_str(0, "As you read the spell book, you find that there are a few spells that will help you.", 0);
	add_dialog_str(1, "Those that can learn now know Fireblast, Arcane Blow, Cloud of Blades and Divine Retribution.", 0);
	add_dialog_str(2, "Those that already knew them were able to learn them better.", 0);
	add_dialog_choice(0, "OK");
	choice = run_dialog(1);
	i = 0;
	while (i < 4) {
		lvl = get_spell_level(i, 0, 16) + 1;
		if (lvl < 5)
			change_spell_level(i, 0, 16, lvl);
		lvl = get_spell_level(i, 0, 19) + 1;
		if (lvl < 5)
			change_spell_level(i, 0, 19, lvl);
		lvl = get_spell_level(i, 1, 15) + 1;
		if (lvl < 5)
			change_spell_level(i, 1, 15, lvl);
		lvl = get_spell_level(i, 1, 17) + 1;
		if (lvl < 5)
			change_spell_level(i, 1, 17, lvl);
		i = i + 1;
	}
break;
